package com.easy.work.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceActivity;


import com.base.syslib.action.SysSession;
import com.base.syslib.pulltorefresh.PullToRefreshBase;
import com.base.syslib.util.DateTimeHelper;
import com.base.syslib.util.TextUtil;
import com.easy.work.bean.UserInfo;
import com.easy.work.utils.SP.SPKey;
import com.easy.work.utils.SP.SharedPreferenceHelper;
import com.easy.work.utils.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zzgpc on 2017-9-6.
 */

public class AppSession extends SysSession {
    private static final String TAG = "AppSession";

    public static String SPTAG = TAG;
    private static Context mContext;
    private static AppSession mInstance = null;


    /**
     * 是否已经完善简历
     */
    private boolean isResumePerfect = false;


    /**
     * 纬度
     */
    private String latitude;
    /**
     * 精度
     */
    private String lontitude;
    private String cityName;
    private String cityCode;
    /**
     * 用户id
     */
    private String userId;

    public UserInfo getUserInfo() {
//        if (userInfo == null) {
//            userInfo = SharedPreferenceHelper.getObject(TAG, SPKey.userInfo, UserInfo.class);
//        }
//        return userInfo;
        return userInfo = SharedPreferenceHelper.getObject(SPTAG, SPKey.userInfo, UserInfo.class);
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
        AppSession.saveAppSessionData(SPKey.userInfo, userInfo);
    }

    private UserInfo userInfo;


    public boolean isResumePerfect() {
        isResumePerfect = AppSession.getSPDataObject("resumePerfect", Boolean.class);
        return isResumePerfect;
    }

    public void setResumePerfect(boolean resumePerfect) {
        AppSession.saveAppSessionData("resumePerfect", resumePerfect);
        isResumePerfect = resumePerfect;
    }

    /**
     * 是否是老师
     */
    private boolean isTeacher = false;

    public AppSession(Context context) {
        mContext = context;
        mInstance = this;

//        mUnLoginActivityList.add("com.easygroup.ngaripatient.publicmodule.selectdoctor.SelectCityAreaActivity");
    }

    public static AppSession getInstance() {
        return mInstance;
    }

    public String getUserId() {
        if (TextUtil.isNull(userId)) {
            userId = AppSession.getAppSPData("userId");
        }

        return userId;
    }

    public void setUserId(String userId) {
        saveAppSessionData("userId", userId);
        this.userId = userId;
//        SPTAG = TAG + "_" + userId;
    }

    public String getLatitude() {
        //120.003404,31.814937
        if (TextUtil.isNull(latitude)) {
            latitude = AppSession.getAppSPData(SPKey.latitude);
        }
        if (TextUtil.isNull(latitude)) {
            latitude = "31.814937";
        }
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
        saveAppSessionData(SPKey.latitude, latitude);
//        SPTAG = TAG + "_" + latitude;
    }

    public String getLontitude() {
        if (TextUtil.isNull(lontitude)) {
            lontitude = AppSession.getAppSPData(SPKey.lontitude);
        }
        if (TextUtil.isNull(lontitude)) {
            lontitude = "120.003404";
        }
        return lontitude;
    }

    public void setLontitude(String lontitude) {
        saveAppSessionData(SPKey.lontitude, lontitude);
        this.lontitude = lontitude;
//        SPTAG = TAG + "_" + lontitude;
    }

    public String getCityCode() {
        if (TextUtil.isNull(cityCode)) {
            cityCode = AppSession.getAppSPData(SPKey.cityCode);
        }
        if (TextUtil.isNull(cityCode)) {
            cityCode = "3204";
        }
        return cityCode;
    }

    public void setCityCodeForName(String cityName) {
        if (StringUtil.isNotEmpty(cityName)) {
            String code = StringUtil.getCityCode(MyApplication.mInstanc, cityName);
            cityCode = code;
            saveAppSessionData(SPKey.cityCode, code);

        }

    }

    public void setCityCode(String cityCode) {
        saveAppSessionData(SPKey.cityCode, cityCode);
        this.cityCode = cityCode;
    }

    public String getCityName() {
        if (TextUtil.isNull(cityName)) {
            cityName = AppSession.getAppSPData(SPKey.cityName);
        }
        if (TextUtil.isNull(cityName)) {
            cityName = "常州市";
        }
        return cityName;
    }

    public void setCityName(String cityName) {

        saveAppSessionData(SPKey.cityName, cityName);
        this.cityName = cityName;
//        SPTAG = TAG + "_" + cityName;
    }


    /**
     * 是否是第一次启动，是否开启导航页面
     *
     * @param
     */
    public void setNaved(Boolean isOpen) {

        saveAppSessionData(SPKey.setting_false, isOpen);
//        SPTAG = TAG + "_" + isOpen;
    }

    /**
     * 是否是第一次启动，是否开启导航页面
     *
     * @param
     */
    public boolean getNaved() {
        try {
            return getSPDataObject(SPKey.setting_false, Boolean.class);
        } catch (Exception e) {
            return false;
        }

    }

    /**
     * 保存一些初始化数据
     *
     * @param key
     * @param object
     */
    public static void saveAppSessionData(String key, Object object) {
        SharedPreferenceHelper.save(SPTAG, key, object);
    }


    /**
     * 获取保存在sp中的数据
     *
     * @param value
     * @return
     */
    public static String getAppSPData(String value) {
        return SharedPreferenceHelper.getObject(SPTAG, value, String.class);
    }

    /**
     * 保存一些初始化数据
     *
     * @param key
     * @param object
     */
    public static void saveAppSessionDataNoId(String key, Object object) {
        SharedPreferenceHelper.save(TAG, key, object);
    }

    /**
     * 获取保存在sp中的数据
     *
     * @param value
     * @return
     */
    public static String getAppSPDataNoId(String value) {
        return SharedPreferenceHelper.getObject(TAG, value, String.class);
    }

    public static <T> T getSPDataObject(String key, Class<T> clazz) {
        return SharedPreferenceHelper.getObject(SPTAG, key, clazz);
    }

    /**
     * 下拉刷新控件时间格式
     */
    public static final String FORMART_REFRESH_TIME = "yyyy-MM-dd HH:mm";

    /**
     * 设置下拉刷新的时间
     *
     * @param pull_refresh_list 下拉刷新控件
     */
    public static void setPullFormartRefreshTime(PullToRefreshBase pull_refresh_list) {
        long currentTime = System.currentTimeMillis();
        pull_refresh_list.setLastUpdatedLabel(DateTimeHelper.formatDate(new Date(currentTime), AppSession.FORMART_REFRESH_TIME));

    }
}
